<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2018/4/22
 * Time: 14:03
 */

namespace app\admin\controller;


use app\admin\model\Admin as AdminModel;

class Admin extends Base
{
    private $model;
    public function _initialize()
    {
        $this->model = new AdminModel();
    }

    /***
     * 管理员列表
     * @return mixed
     */
    public function index(){
        if (request()->isAjax()) {
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['searchText'])) {
                $where['user_name'] = ['like', '%' . $param['searchText'] . '%'];
            }
            $where['status'] = 0;
            $selectResult = $this->model->getAdminByWhere($where, $offset, $limit);

            $status = config('admin_status');

            //拼接参数
            foreach ($selectResult as $k => $v) {
                $selectResult[$k]['last_login_time'] = date('Y-m-d H:i:s',$v['last_login_time']);
                $selectResult[$k]['status'] = $status[$v['status']];
                if (1 == $v['id']) {
                    $selectResult[$k]['operate'] = '';
                    continue;
                }
                $selectResult[$k]['operate'] = showOperate($this->makeButton($v['id']));
            }
//            dump($selectResult);exit;
            $result['total'] = $this->model->getAllAdmin($where);//总数据
            $result['rows'] = $selectResult;
//            dump($result);die;
            return json($result);
        }
        return $this->fetch();
    }

    /***
     * 添加管理员
     * @return mixed|\think\response\Json
     */
    public function add(){
        if (request()->isPost()){
            $param = input('post.');
            $res = $this->model->checkName($param['user_name']);
            if($res){
                return json(msg($res['code'], $res['data'], $res['msg']));
            }
            $param['password'] = md5($param['password'] . config('salt'));
            $param['head'] = '/static/admin/images/profile_small.jpg'; // 默认头像

            $flag = $this->model->insertAdmin($param);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $this->assign('status',config('admin_status'));
        return $this->fetch();
    }

    public function edit(){
        if (request()->isPost()) {
            $param = input('post.');
            if (empty($param['password'])) {
                unset($param['password']);
            } else {
                $param['password'] = md5($param['password'] . config('salt'));
            }
            $flag = $this->model->editAdmin($param);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $id = input('param.id');
        $this->assign([
            'status' => config('admin_status'),
            'admin' => $this->model->getOneAdmin($id)
        ]);
        return $this->fetch();
    }

    /**
     * 删除管理员
     * @return \think\response\Json
     */
    public function del()
    {
        $id = input('param.id');

        $flag = $this->model->delAdmin($id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '编辑' => [
                'auth' => 'Admin/edit',
                'href' => url('Admin/edit', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '删除' => [
                'auth' => 'Admin/del',
                'href' => "javascript:adminDel(" .$id .")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }
}